import defaultImg from '@/components/defaultImg/defaultImg.vue'
import basicInformation from './components/basicInformation.vue' //基本信息
import maintenanceRecords from './components/maintenanceRecords.vue' //保养记录
import vehicleProfile from './components/vehicleProfile.vue' //车况档案
import inspectionRecord from './components/inspectionRecord.vue' //检查记录
export default {
  components: {
    defaultImg,
    basicInformation,
    inspectionRecord,
    maintenanceRecords,
    vehicleProfile
  },
  data() {
    return {
      carllist: {}, //车子信息
      stiplist: [], //店铺信息
      carid: '', //车子id
      xedColorList: [], //颜色list

      specifAllbrand: {
        brandlist: [], //品牌
        Serieslist: [], //系列
        Displacement: [], //排量
        Modelyear: [], //年款
        Configulist: [], //车子
      },
      list: [],
      carList: {
        car_Type_Id: 3841, //车图片--系列id
      },
      isIcon: false, //删除按钮显示
      currentIndex: 0,
      inspectstate: 0,
      id: 0,
      editname: '删除',
      derfimg: true, //是否显示默认图
      pageindex: 1,
      noInformation: false, //暂无信息
      defaultImg: false, //默认图
      isFixedTop: false,
      Topdistance: 3000,
      lists: [{
        name: '基本信息',
        icon: 'iconjibenxinxi',
        eg: 'a'
      },
      {
        name: '保养记录',
        icon: 'iconbaoyangjilu',
        eg: 'b'
      },
      {
        name: '车况档案',
        icon: 'iconchekuangdangan_1',
        eg: 'c'
      },
      {
        name: '检查记录',
        icon: 'iconjianchajilu',
        eg: 'd'
      },
      {
        name: '保养超值礼包',
        icon: 'iconchaozhilibao',
        eg: 'e'
      }
      ],
      current: 0,
      orderid: '',
      showPage: false
    }
  },
  created() {},
  onLoad(caridxix) {
    this.currentIndex = Number(caridxix.index) //第几个车子
    this.current = Number(caridxix.current) //滑块定位
    setTimeout(() => {
      this.orderid = caridxix.oderid
      this.inspectstate = caridxix.state //检查记录内部滑块定位
    }, 500)
    // 选择品牌，系列，排量，年款，车子
    uni.$on('mylovercarinformation', data => {
      Object.assign(this.specifAllbrand, data)
      this.carList.car_Type_Id = 3841
      if (data.licenseplate && data.pageFrom === 'mylovercarinformation') {
        this.carList.car_Type_Id = this.specifAllbrand.Serieslist.carTypeId //系列id
        this.AddCar()
      }
    })
    //车子id
    this.GetCarList() //获取车单列表
    // 吸顶
    uni.pageScrollTo({
      scrollTop: 0,
      duration: 0
    })
    setTimeout(() => {
      this.GetTop()
    }, 1000)
  },
  onPageScroll(e) {
    // console.log(e.scrollTop)
    if (e.scrollTop > this.Topdistance) {
      this.isFixedTop = true
    } else {
      this.isFixedTop = false
    }
    uni.$emit('storePageScroll', {
      e,
      current: this.current
    })
  },
  onReachBottom() {
    uni.$emit('storeLoadMore', this.current)
  },
  onUnload() {
    uni.$off('storeLoadMore')
    uni.$off('storePageScroll')
    uni.$off('mylovercarinformation')
  },
  methods: {
    // 点击导航栏右侧
    onNavigationBarButtonTap(e) {
      /**
       * 向本地存储中设置selecerequirments的值，即启动标识；
       */
      uni.setStorage({
        key: 'selecerequirments',
        data: true,
      })
      uni.switchTab({
        url: '/pages/index/index'
      })
    },
    //添加我d爱车
    async AddCar() {
      const value = uni.getStorageSync('Ztc_C_USER_INFO')
      if (value) {
        var ids
        ids = value.id
      }
      try {
        //添加爱车重复，退出添加
        this.list.forEach((item, index) => {
          if (item.car_Id === this.specifAllbrand.Configulist.id) {
            uni.showToast({
              title: '添加重复，请重新选择',
              icon: 'none'
            })
            throw new Error('EndIterative')
          }
        })
        let data = {
          uid: ids,
          brand_Id: this.specifAllbrand.brandlist.bid,
          brand_Name: this.specifAllbrand.brandlist.brand,
          car_Type_Id: this.specifAllbrand.Serieslist.carTypeId,
          car_Type_Name: this.specifAllbrand.Serieslist.carTypeName,
          car_Id: this.specifAllbrand.Configulist.id,
          car_Name: this.specifAllbrand.Configulist.name,
          car_Num: this.specifAllbrand.licenseplate, //车牌号
          mileage: 0, //里程 /km
          vin: '', //vin号
          engine: '', //发动机号
          displacement: this.specifAllbrand.Displacement.pName,
          yeartype: this.specifAllbrand.Modelyear.years
        }
        const res = await this.$api.common.AddCar(data)
        if (res.code === 200) {
          this.GetCarList() //获取车单
        }
      } catch (e) {
        if (e.message !== 'EndIterative') throw e
      }
    },
    //我的爱车列表
    async GetCarList() {
      const value = uni.getStorageSync('Ztc_C_USER_INFO')
      if (value) {
        var ids
        ids = value.id
      }
      try {
        let data = {
          pageIndex: this.pageindex,
          pageSize: 10,
          uid: ids
        }
        const res = await this.$api.commodity.GetCarList(data)
        //如果总条数为0，说明没有车单，必须跳转品牌，选择车
        if (res.data.total === 0) {
          uni.navigateTo({
            url: '/pages_common/addYourCar/addYourCar?type=120&pageFrom=mylovercarinformation'
          })
        } else {
          this.list = res.data.list

          // 压缩图片
          this.list.forEach(item => {
            item.car_Tyoe_img += '!clientdetail'
          })
          this.$nextTick(() => {
            this.carid = res.data.list[this.currentIndex].id
          })
          // this.GetCarLoanPaymentList(JSON.stringify(this.list[0].configuration_Id))

        }
        this.showPage = true
      } catch (e) {
        console.log(e)
      }
    },
    //获取价格，店铺等
    // async GetCarLoanPaymentList(id) {
    //   try {
    //     let data = {
    //       'configuration_Id': id, //车辆ID
    //       'lon': 0, //经度
    //       'lat': 0 //纬度
    //     }
    //     const res = await this.$api.commodity.GetCarLoanPaymentList(data)
    //     if (res.data.id === null) {
    //       this.noInformation = false
    //       this.defaultImg = false
    //     } else {
    //       this.noInformation = true
    //       this.defaultImg = true
    //       // 所有信息 店铺信息 颜色信息
    //       this.$nextTick(() => {
    //         this.carid = res.data.goods_Carsource_Id
    //         this.stiplist = res.data.goodsCarSourceStoreDetailEntity
    //         this.carllist = res.data
    //         this.xedColorList = this.filtercolor(res.data.colorList) //颜色
    //       })
    //       this.xedColorList.forEach(item => {
    //         item.chekd = false
    //       })
    //     }
    //   } catch (e) {}
    // },
    //图片报错
    imageError: function(e) {
      if (e.type === 'error') {
        // this.derfimg = false
        // <image src="https://files.yzsheng.com/client/comparison/images/che.png"></image>
      }
    },
    // 删除我的爱车
    async delcars(e) {
      let data = {
        id: e
      }
      const res = await this.$api.common.DeleteCar(data)
      this.GetCarList() //获取车单列表
    },
    //编辑
    async edit() {
      this.isIcon = !this.isIcon
      if (this.isIcon) {
        this.editname = '完成'
      } else {
        this.editname = '删除'
      }
    },
    //添加配置
    addConf() {
      uni.navigateTo({
        url: '/pages_common/addYourCar/addYourCar?type=120&pageFrom=mylovercarinformation'
      })
    },
    //轮播定位
    swierChange(e) {
      this.carid = e.target.currentItemId
      // this.GetCarLoanPaymentList(e.target.currentItemId) //获取价格，店铺等
      this.currentIndex = e.detail.current
    },
    trandstioncs(e) {
      this.pageindex = ++this.pageindex
      this.GetCarFullPaymentList()
    },
    //分割颜色
    filtercolor(data) {
      data.forEach(item => {
        var arr = item.inColour.colorCoding.split('/')
        item.inColour.neiColor = arr
      })
      return data
    },
    //查看配置
    lookpeiclick() {
      uni.navigateTo({
        url: '/pages_commodity/payDeposit/payDeposit'
      })
    },
    GetTop() {
      //获取元素距离顶部的距离
      var _this = this
      uni.getSystemInfo({
        success: resu => {
          const query = uni.createSelectorQuery()
          query.select('#box').boundingClientRect()
          query.selectViewport().scrollOffset()
          query.exec(function(res) {
            _this.Topdistance = res[0].top
          })
        },
        fail: res => {}
      })
    },
    //点击方案
    change(index) {
      this.current = index

      //跳转福利购
      if (index === 4) {
        this.current = 0 //福利购返回到基础数据
        uni.navigateTo({
          url: '/pages_buy_index/welfareDetails/welfareDetails'
        })
      }
    },
  }
}
